<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Controller;
use think\Config;
use think\Db;

class Index extends IndexBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->fetch();
    }

    public function lists()
    {
        $record = Db::name('dp_type')->field('id,name,desc,img')->where('id',1)->find();
        $childRecord = Db::name('dp_type')->field('id,name')->where('pid',1)->select();
        if($childRecord){
            foreach($childRecord as &$v){
                $v['img'] = Db::name('dp_record')->where('type',$v['id'])->order('sort','asc')->value('img');
            }
        }
        return $this->fetch('',[
            'record' => $record,
            'childRecord' => $childRecord
        ]);
    }

  public function upload()
{
    $config = [
        'size' => 1209715200, // 200MB，建议根据需要调整
        'ext'  => 'jpg,jpeg,png,gif,mp4,mov,avi' // 加上视频扩展名
    ];

    // 获取上传的文件
    $file = $this->request->file('file');
    if (!$file) {
        return json(['code' => 0, 'msg' => '未上传文件']);
    }

    // 设置上传路径
    $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');

    // 执行上传
    $info = $file->validate($config)->move($upload_path);

    if ($info) {
        return json([
            'code' => 1,
            'msg' => '上传成功',
            'data' => [
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ]
        ]);
    } else {
        return json(['code' => 0, 'msg' => $file->getError()]);
    }
}

    public function clists()
    {
        $id = input('id');
        $cate = Db::name('dp_type')->field('id,name,desc,img')->where('id',$id)->find();
        $record = Db::name('dp_record')->field('id,name,img')->where('type',$id)->order('sort','asc')->select();

        return $this->fetch('',[
            'cate' => $cate,
            'record' => $record
        ]);
    }

    public function detail()
    {
        $id = input('id');
        $record = Db::name('dp_record')->field('id,name,desc,img,video')->where('id',$id)->find();

        return $this->fetch('',[
            'record' => $record
        ]);
    }
}
